package cn.com.checne.controller.front;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.checne.constants.Constants;
import cn.com.checne.dao.NewsInfoDao;
import cn.com.checne.model.domain.NewsInfo;
import cn.com.checne.model.dto.NewsQuery;
import cn.com.checne.util.LanguageUtils;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;

@Controller
public class RSSFrontController {
	protected static final Logger logger = Logger
			.getLogger(RSSFrontController.class);

	@Resource
	private NewsInfoDao newsDao;

	// RSS
	@RequestMapping(value = "/rss", method = RequestMethod.GET)
	public void rss(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap,String lg) throws Exception {
//		String lg = StringUtils.isBlank(request.getParameter("lg")) ? Constants.LANGUAGE_CN : request.getParameter("lg");
		int languageId = LanguageUtils.getLanguage(lg);
		String lgName = languageId == 1 ? "cn" : "en";
		String projectPath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath() + "/";
		response.setContentType("text/xml; charset=UTF-8");
		SyndFeed feed = new SyndFeedImpl();
		feed.setFeedType("rss_2.0");
		feed.setTitle("CHINA  HUADIAN ENGINEERING CO., LTD. NEWS");
		feed.setLink(projectPath + lg + "/news");
		feed.setDescription("CHINA  HUADIAN ENGINEERING CO., LTD. NEWS");

		try {
			List<SyndEntry> entries = new ArrayList<SyndEntry>();
			SyndEntry entry = null;
			SyndContent content = null;
			NewsQuery query = new NewsQuery();
			query.setLanguageId(languageId);
			List<NewsInfo> newsInfoList = newsDao.getNewsImagesListByCondition(
					query, 0, 30);
			for (NewsInfo newsInfo : newsInfoList) {
				entry = new SyndEntryImpl();
				entry.setTitle(newsInfo.getTitle());
				entry.setLink(projectPath + lgName + "/news/"
						+ newsInfo.getNewsId());
				entry.setAuthor(newsInfo.getAuthor());
				entry.setPublishedDate(newsInfo.getPublishDate());

				content = new SyndContentImpl();
				content.setType("text/plain");
				content.setValue(newsInfo.getContent());
				entry.setDescription(content);

				entries.add(entry);
			}
			feed.setEntries(entries);
			SyndFeedOutput output = new SyndFeedOutput();
			output.output(feed, response.getWriter());
		} catch (FeedException ex) {
		} catch (Exception ex) {
		}
	}
}
